/**
 * 品牌系数-分类保内系数编辑
 * @author linfeng
 * @since 2019/11/24 16:06
 */
layui.define(['form', 'layedit', 'laydate'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let laydate = layui.laydate;

    let effectiveDateObj = {
        elem: '#effectiveDate',
        type: 'date',
        btns: ['clear', 'confirm'],
        min: 1
    };

    laydate.render(effectiveDateObj);

    form.render(null, "zmn-form");
    zmnLayUtil.layFormVerify(form);

    $("#categOneRatioInput").bind("input propertychange",function(event){
        if ($("input[type='checkbox']").is(':checked')){
            $("input[type='checkbox']").attr('checked', false);
            form.render(null, "zmn-form");
        }
    });

    form.on('checkbox(citypriceratiobox)', function(data){
        let citypriceratiobox = document.getElementsByName("citypriceratiobox");
        let categOneRatio = document.getElementsByName("categOneRatio");
        if (citypriceratiobox[0].checked){
            let categTwoRatio = document.getElementsByName("categTwoRatio");
            for(var i=0; i < categTwoRatio.length; i++){
                categTwoRatio[i].value=categOneRatio[0].value;
            }
        }
    });

    Date.prototype.format = function(fmt) {
        var o = {
            "M+" : this.getMonth()+1,                 //月份
            "d+" : this.getDate(),                    //日
            "h+" : this.getHours(),                   //小时
            "m+" : this.getMinutes(),                 //分
            "s+" : this.getSeconds(),                 //秒
            "q+" : Math.floor((this.getMonth()+3)/3), //季度
            "S"  : this.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt)) {
            fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
        }
        for(var k in o) {
            if(new RegExp("("+ k +")").test(fmt)){
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
            }
        }
        return fmt;
    }

    form.on('submit(zmn-form-reset)', function (obj) {
        form.val("zmn-form", {
            "categOneRatio": "" // "name": "value"
            ,"categTwoRatio": ""
            ,"citypriceratiobox": ""
        });
        return false;
    });

    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        if(!verify()){
            return false;
        }

        var openId = layer.open({type: 3});
        let data = new Object();

        let innerArr = new Array();
        let categOneRatio = document.getElementsByName("categOneRatio");
        let categOneRatioObject = new Object();
        categOneRatioObject["categOneId"] = json.categOneId;
        categOneRatioObject["bizType"] = json.bizType;
        categOneRatioObject["priceType"] = json.priceType;
        categOneRatioObject["toBeEffectiveRatio"] = categOneRatio[0].value;
        categOneRatioObject["brandId"] = json.brandId;
        categOneRatioObject["channelId"] = json.channelId;
        categOneRatioObject["servCategId"] = json.servCategId;

        let effectiveDateStr = null;
        if (obj.field.effectiveDate == null || obj.field.effectiveDate == '' || obj.field.effectiveDate == undefined) {
            var nowDate = new Date();
            effectiveDateStr = new Date(nowDate.getTime() + 24*60*60*1000).format("yyyy-MM-dd");
        } else {
            effectiveDateStr = obj.field.effectiveDate;
        }

        categOneRatioObject["effectiveDate"] = effectiveDateStr;

        let categTwoRatio = document.getElementsByName("categTwoRatio");
        for(var i=0; i < categTwoRatio.length; i++){
            let inner = new Object();
            inner["categOneId"] = json.categOneId;
            inner["brandId"] = json.brandId;
            inner["channelId"] = json.channelId;
            inner["bizType"] = json.bizType;
            inner["priceType"] = json.priceType;
            inner["servCategId"] = json.servCategId;
            inner["categId"] = categTwoRatio[i].title;
            inner["toBeEffectiveRatio"] = categTwoRatio[i].value;
            inner["effectiveDate"] = effectiveDateStr;
            innerArr.push(inner);
        }
        data["categTwoBrandRatioList"] = innerArr;
        data["categOneBrandRatio"] = categOneRatioObject;
        var url = "/channel/brand/ratio/categoryRatioAddEdit.action";

        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success:function(result) {
                layer.close(openId);
                if (result.status == 200) {
                    layer.msg(result.message);
                    setTimeout(function(){
                        window.location.href = "/channel/brand/ratio/categoryRatio.action?channelId="+json.channelId+"&brandId="+json.brandId+"&priceType="+json.priceType+"&bizType="+json.bizType+"&servCategId="+json.servCategId;
                    }, 1000);
                } else {
                    layer.msg(result.message, {"icon": 2});
                }
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                layer.close(openId);
            }
        })
        return false;
    });

    //对外暴露的接口
    exports('channel_brand_category_ratio_edit', {});
});

function verify() {
    let categOneRatio = document.getElementsByName("categOneRatio");

    var reg=/^\d+(\.\d{1,2})?$/;

    if (categOneRatio[0].value != null && categOneRatio[0].value != undefined && categOneRatio[0].value != ""){
        if(!reg.test(categOneRatio[0].value))
        {
            layer.alert("请输入整数或者1到2位小数");
            return false;
        }

        if (parseFloat(0.6) <= parseFloat(categOneRatio[0].value) && parseFloat(3.0) >= parseFloat(categOneRatio[0].value)){
        } else{
            layer.alert("一级分类系数值不符合规定,系数值范围0.6~3.0");
            return false;
        }
    }

    let categTwoRatio = document.getElementsByName("categTwoRatio");
    for(var i=0; i < categTwoRatio.length; i++){
        if (categTwoRatio[i].value == null || categTwoRatio[i].value == ""){
        } else{
            if(!reg.test(categTwoRatio[i].value)){
                layer.alert("请输入整数或者1到2位小数");
                return false;
            }
            if (parseFloat(0.6) <= parseFloat(categTwoRatio[i].value) && parseFloat(3.0) >= parseFloat(categTwoRatio[i].value)){
            } else{
                layer.alert("二级分类系数值不符合规定,系数值范围0.6~3.0");
                return false;
            }
        }
    }

    return true;
}

function cancel() {
    let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index);
}